﻿using NetDimension.NanUI;
using NetDimension.NanUI.Browser;
using NetDimension.NanUI.JavaScript;
using PLM.ConfigurationCenter.Core.Expansion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PLM.Configuration.Center.NanUIApp.Formiums {
    public class FormiumEvent {
        /// <summary>
        /// 
        /// </summary>
        public Formium _formium;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="formium"></param>
        public FormiumEvent(Formium formium) {
            _formium = formium;
        }
        /// <summary>
        /// 注册公共事件
        /// </summary>
        public void RegisterCommonEvent() {
            _formium.BeforeClose += BeforeClose!;
            _formium.KeyEvent += KeyEvent!;
        }
        /// <summary>
        /// 窗体关闭前提示信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BeforeClose(object sender, FormiumCloseEventArgs e) {
            if (MessageBox.Show("是否确定关闭?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No) {
                e.Canceled = true;
            }
        }
        /// <summary>
        /// 注册按键事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void KeyEvent(object sender, NetDimension.NanUI.Browser.KeyEventArgs e) {
            // Force reload the page when F5 is pressed 
            // 注册F5键按下时强制刷新页面
            if (e.KeyEvent.WindowsKeyCode == (int)Keys.F5) {
                _formium.Reload(true);
            }

            // Show DevTools when F12 is pressed
            // 注册F12键按下时打开开发者工具
            if (e.KeyEvent.WindowsKeyCode == (int)Keys.F12) {
                _formium.ShowDevTools();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jsObject"></param>
        /// <param name="action"></param>
        public void RegisterMethod<T>(JavaScriptObject jsObject, string actionName, Func<string, T> action) where T : class, new() {
            jsObject.Add(actionName, args => {
                var msg = args.FirstOrDefault(x => x.IsString)!.GetString();
                T data = new();
                _formium.InvokeIfRequired(() => {
                    data = action.Invoke(msg);
                });
                return new JavaScriptValue(data.ToJson());
            });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jsObject"></param>
        /// <param name="action"></param>
        public void RegisterMethod(JavaScriptObject jsObject, string actionName, Action action) {
            //注册同步方法launchDemo02，用于在start_window对应的startui.html中点击按钮跳转
            jsObject.Add(actionName, args => {
                _formium.InvokeIfRequired(action);
                return null;
            });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jsObject"></param>
        /// <param name="action"></param>
        public void RegisterMethod<T>(JavaScriptObject jsObject, string actionName, Action<string> action) {
            //注册同步方法launchDemo02，用于在start_window对应的startui.html中点击按钮跳转
            jsObject.Add(actionName, args => {
                var msg = args.FirstOrDefault(x => x.IsString)!.GetString();
                _formium.InvokeIfRequired(() => {
                    action.Invoke(msg);
                });
                return null;
            });
        }
    }
}
